from autoTest_brtcApi.core.usercenter_apiUtils import userApiUtil
from autoTest_brtcApi.utils.write import write_yaml
from autoTest_brtcApi.utils.read import readFile

## 在该模块的方法里面，构造请求头和参数
headers = {"Content-Type": "application/json",
           "Accept": "application/json"
           }


class UserApi():

    def reg_api(self, username, password, phone):
        ## 注册

        ## 请求头
        params = {
            "user_name": username,
            'passwd': password,
            'phone': phone
        }
        # params = {
        #     "user_name": "zqf8937",
        #     'passwd': 'uiadjw',
        #     'phone': '15478945632'
        # }
        ## 接收接口返回的数据
        # res = requests.post(url=reg_url,headers=headers,json=params)
        res_reg = userApiUtil.reg_util(headers=headers, json=params)
        return res_reg

    def login_api(self,password,phone):
        ## 登录

        params = {
            "passwd": password,
            "phone": phone
        }

        res_login = userApiUtil.res_util(headers=headers, json=params)
        if res_login.json()['code'] ==0:
        ##把动态的userId和token值存放到yaml文件中
            userId = res_login.json()["data"]["userId"]
            token = res_login.json()["data"]["token"]
        content = {"userId": str(userId),
                   "token": token}

        write_yaml("tmp.yaml", content)

        return res_login

    def modifyUserNick(self):
        #读取tmp.yaml,获取userID和token
        response = readFile("tmp.yaml")
        headers_modifyUserNick = {"userID": str(response["userId"]),
                                  "token": response["token"]
                                  }
        params = {"nickName": "ijkvhy"}

        res = userApiUtil.res_modifyUserNick_util(headers_modifyUserNick, data=params)
        return res


## 实例化 UserApi
userApi = UserApi()
